package crm.model.teacherPC.net;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.Thread.State;
import java.net.Socket;
import java.util.ArrayList;
import java.util.LinkedList;

import crm.model.NetConst;
import crm.model.teacherPC.Manager;
import crm.model.teacherPC.bannedList.ApplicationBanned;
import crm.model.teacherPC.bannedList.BrowserBanned;
import crm.model.teacherPC.net.job.CursorPositionJob;
import crm.model.teacherPC.net.job.ForeGroundWindowJob;
import crm.model.teacherPC.net.job.Job;
import crm.model.teacherPC.net.job.MessageJob;
import crm.model.teacherPC.net.job.ScreenLockJob;
import crm.model.teacherPC.net.job.ScreenShotJob;
import crm.model.teacherPC.net.job.UserJob;
import crm.model.teacherPC.net.job.WindowNameJob;


public class Transfer implements Runnable {

	public static ArrayList<String> filterList = new ArrayList<String>();
	
	private StudentPC client;
	private final Socket socket;
	private ObjectInputStream is;
	private ObjectOutputStream os;
	boolean closeBrowser ;
	boolean closeApp;
	private final LinkedList <Job> jobs;
	BrowserBanned browserBannedList;
	ApplicationBanned appBannedList;
	private final StudentPC pc;
	Thread thread;
	public Transfer(Socket socket, Manager manager, StudentPC pc){
		this.socket = socket;
		this.pc=pc;
		closeBrowser = false;
		closeApp = false;
		browserBannedList = manager.getBrowserbannList();
		appBannedList = manager.appbannList;
		jobs = new LinkedList<Job>();
		try {
			is = new ObjectInputStream(socket.getInputStream());
			os = new ObjectOutputStream(socket.getOutputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		thread = new Thread(this);
		thread.start();
	}
	
	public void run() {
		while(!jobs.isEmpty()){
			System.out.println("Size of the Job list: " + jobs.size());
			Job job = jobs.remove();
			job.doing(is, os);
			
		}
	}
	
	
	
	public void getImage(){
		jobs.add(new ScreenShotJob(pc));
		System.out.println("JOBlist: " + jobs.size() + "\nthreadstate: " + thread.getState().name());
		if (thread.getState() == State.TERMINATED) (thread = new Thread(this)).start();
	}
	
	public void getWindowName(){

		jobs.add(new WindowNameJob(browserBannedList, appBannedList, pc, closeBrowser, closeApp));
		
		if (thread.getState() == State.TERMINATED) (thread = new Thread(this)).start();
	}
	public void setCursorPos(int x, int y){
		jobs.add(new CursorPositionJob(x,y));
		if (thread.getState() == State.TERMINATED) (thread = new Thread(this)).start();
	}
	public void getUser(){
		jobs.add(new UserJob(pc));
		if (thread.getState() == State.TERMINATED) (thread = new Thread(this)).start();
	}
	public void lockScreen(){
		jobs.add(new ScreenLockJob(false));
		if (thread.getState() == State.TERMINATED) (thread = new Thread(this)).start();
	}
	public void unLockScreen(){
		jobs.add(new ScreenLockJob(true));
		if (thread.getState() == State.TERMINATED) (thread = new Thread(this)).start();
	}
	public void close(){
		try {
			os.writeObject(NetConst.CLOSE);
			socket.close();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void sendMessage(String message) {
		jobs.add(new MessageJob(message));
		if (thread.getState() == State.TERMINATED) (thread = new Thread(this)).start();
		
	}
	public boolean containsInFilter(String name){
		return filterList.contains(name);
	}

	public void getForegroundWindowName() {
		jobs.add(new ForeGroundWindowJob(pc));
		if (thread.getState() == State.TERMINATED) (thread = new Thread(this)).start();
	}
	public String getIP(){
		return socket.getInetAddress().getHostAddress();
	}


}
